#include <QScrollBar>
#include <QMouseEvent>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initUI();
    initMenuBar();
    initMainToolBar();
    initTabWidget();
    initTreeWidget();

    m_bDrawRoad = false;
    m_pCurrentDrawRoadItem = nullptr;
    m_pCurrentDrawRectRoadItem = nullptr;
    m_pCurrentDrawJuncionItem = nullptr;

    iniSignalSlots();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::iniSignalSlots()
{
    QObject::connect(ui->mainWndView, SIGNAL(mouseMovePoint(QPoint)), this, SLOT(on_mouseMovePoint(QPoint)));
    QObject::connect(ui->mainWndView, SIGNAL(mouseReleasePoint(QPoint)), this, SLOT(on_mouseReleasePoint(QPoint)));
    QObject::connect(ui->mainWndView, SIGNAL(mouseClicked(QPointF)), this, SLOT(on_mouseClicked(QPointF)));
    QObject::connect(ui->mainWndView, SIGNAL(mouseDoubleClick(QPoint)), this, SLOT(on_mouseDoubleClick(QPoint)));

    QObject::connect(m_pActionMeanAbout, SIGNAL(triggered()), this, SLOT(on_ActionMeanAbout()));
}

void MainWindow::initUI()
{
    m_pScene = new QGraphicsScene(-32000, -24000, 64000, 48000); //创建QGraphicsScene
    ui->mainWndView->setScene(m_pScene); //与view关联
}

void MainWindow::initMenuBar()
{
    m_pActionMeanNew = new QAction("新建", this);
    m_pActionMeanOpen = new QAction("打开", this);
    m_pActionMeanSave = new QAction("保存", this);
    m_pActionMeanSaveAs = new QAction("另存为", this);
    m_pActionMeanRename = new QAction("重命名", this);
    m_pActionMeanDataGeneration = new QAction("数据生成", this);
    m_pActionMeanInput = new QAction("导入", this);
    m_pActionMeanOutput = new QAction("导出", this);
    m_pActionMeanBaseMapInput = new QAction("底图导入", this);
    m_pActionMeanQuit = new QAction("退出", this);
    QList<QAction*> listActionsUp, listActionMid, listActionDown;
    listActionsUp.append(m_pActionMeanNew);
    listActionsUp.append(m_pActionMeanOpen);
    listActionsUp.append(m_pActionMeanSave);
    listActionsUp.append(m_pActionMeanSaveAs);
    listActionsUp.append(m_pActionMeanRename);

    listActionMid.append(m_pActionMeanDataGeneration);
    listActionMid.append(m_pActionMeanInput);
    listActionMid.append(m_pActionMeanOutput);
    listActionMid.append(m_pActionMeanBaseMapInput);

    listActionDown.append(m_pActionMeanQuit);
    ui->menuFile->addActions(listActionsUp);
    ui->menuFile->addSeparator();
    ui->menuFile->addActions(listActionMid);
    ui->menuFile->addSeparator();
    ui->menuFile->addActions(listActionDown);

    // 帮助
    m_pActionMeanAbout = new QAction("关于", this);
    QList<QAction*> listActionHelp;
    listActionHelp.append(m_pActionMeanAbout);
    ui->menuHelp->addActions(listActionHelp);
}

void MainWindow::initMainToolBar()
{
    QList<QAction*> listActions;
    m_pActionToolNew = new QAction("新建", this);
    m_pActionToolOpen = new QAction("打开", this);
    m_pActionToolSave = new QAction("保存", this);
    m_pActionToolZoomOutMap = new QAction("缩放", this);
    m_pActionToolZoomInMap = new QAction("放大", this);
    m_pActionToolShowAll = new QAction("显示全部", this);
    listActions.append(m_pActionToolNew);
    listActions.append(m_pActionToolOpen);
    listActions.append(m_pActionToolSave);
    listActions.append(m_pActionToolZoomOutMap);
    listActions.append(m_pActionToolZoomInMap);
    listActions.append(m_pActionToolShowAll);
    ui->mainToolBar->addActions(listActions);
}

void MainWindow::initTabWidget()
{
    ui->tabWidget->setFixedWidth(180);
    ui->tabWidget->removeTab(0);
    ui->tabWidget->removeTab(0);
    ui->tabWidget->tabBar()->setStyle(new CustomTab);

    m_pWidgetElementRoad = new QWidget(this);
    m_pPushBtnLineLane = new QPushButton("直道", this);
    m_pPushBtnCurveLane = new QPushButton("弯道", this);
    m_pPushBtnJoinLane = new QPushButton("交汇口", this);
    QVBoxLayout *pVBLayoutPushBtn = new QVBoxLayout();
    pVBLayoutPushBtn->addWidget(m_pPushBtnLineLane);
    pVBLayoutPushBtn->addWidget(m_pPushBtnCurveLane);
    pVBLayoutPushBtn->addWidget(m_pPushBtnJoinLane);
    pVBLayoutPushBtn->addStretch();

    m_pWidgetElementRoad->setLayout(pVBLayoutPushBtn);
    ui->tabWidget->addTab(m_pWidgetElementRoad, "道\n路");

    m_pWidgetElementTrafficSign = new QWidget(this);
    ui->tabWidget->addTab(m_pWidgetElementTrafficSign, "交\n通\n标\n志");

    m_pWidgetElementPavementSign = new QWidget(this);
    ui->tabWidget->addTab(m_pWidgetElementPavementSign, "路\n面\n标\n志");

    m_pWidgetElementTrafficLight = new QWidget(this);
    ui->tabWidget->addTab(m_pWidgetElementTrafficLight, "交\n通\n灯");
}

void MainWindow::initTreeWidget()
{
    QFont font("宋体", 24);
    QFontMetrics fm(font);
    int ipixelsWide = fm.width("属性编辑区");
    int iToolBoxWidth = ui->toolBox->width();
    ui->toolBox->setStyleSheet(QString("QToolBox::tab {padding-left: %1px;}").arg((iToolBoxWidth - ipixelsWide) / 2));
    ui->toolBox->removeItem(0);
    ui->toolBox->removeItem(0);

    m_pPropertiesDialog = new PropertiesDialog();
    m_pPropertiesDialog->setFixedWidth(500);
    ui->toolBox->addItem(m_pPropertiesDialog, "属性编辑区");
}

void MainWindow::on_actionRoad_triggered()
{
    if(!m_bDrawRoad)
    {
        m_bDrawRoad = true;

        m_pCurrentDrawRoadItem = new DrawRoadItem(this);;
        m_pScene->addItem(m_pCurrentDrawRoadItem);
    }
    else
    {
        m_bDrawRoad = false;
        m_pCurrentDrawRoadItem = nullptr;
        ui->mainWndView->update();
    }
}

void MainWindow::on_actionStraightWay_triggered()
{
    if(!m_bDrawRoad)
    {
        m_bDrawRoad = true;

        m_pCurrentDrawRectRoadItem = new DrawRectRoadItem(this);;
        m_pScene->addItem(m_pCurrentDrawRectRoadItem);

        connect(m_pCurrentDrawRectRoadItem, SIGNAL(sendLink(QRectF)), this, SLOT(onSendRect(QRectF)));
    }
    else
    {
        m_bDrawRoad = false;
        m_pCurrentDrawRectRoadItem = nullptr;
        ui->mainWndView->update();
    }
}

void MainWindow::on_actionLink_triggered()
{
    if(!m_bDrawRoad)
    {
        m_bDrawRoad = true;

        m_pCurrentDrawLinkItem = new DrawLinkItem(this);
        connect(this, SIGNAL(setLinkRect(QRectF)), m_pCurrentDrawLinkItem, SLOT(setRect(QRectF)));
        m_pScene->addItem(m_pCurrentDrawLinkItem);
    }
    else
    {
        m_bDrawRoad = false;
        m_pCurrentDrawLinkItem = nullptr;
        ui->mainWndView->update();
    }
}

void MainWindow::on_actionJunction_triggered()
{
    if(!m_bDrawRoad)
    {
        m_bDrawRoad = true;

        m_pCurrentDrawJuncionItem = new DrawJunciontItem(this);
        connect(this, SIGNAL(setLinkRect(QRectF)), m_pCurrentDrawJuncionItem, SLOT(setRect(QRectF)));
        m_pScene->addItem(m_pCurrentDrawJuncionItem);
    }
    else
    {
        m_bDrawRoad = false;
        m_pCurrentDrawJuncionItem = nullptr;
        ui->mainWndView->update();
    }
}

void MainWindow::on_mouseMovePoint(QPoint point)
{
//    if(m_bDrawRoad && m_pCurrentDrawRoadItem != nullptr)
//    {
//        QPointF pointScene = ui->mainWndView->mapToScene(point);
//        m_pCurrentDrawRoadItem->addPoint(pointScene);
//    }
}

void MainWindow::on_mouseReleasePoint(QPoint point)
{
    if(m_bDrawRoad && m_pCurrentDrawRoadItem != nullptr)
    {
//        QPointF pointScene = ui->mainWndView->mapToScene(point);
//        m_pCurrentDrawRoadItem->addPoint(point);
    }
}

//void MainWindow::on_mouseMove(QPoint point)
//{
//    if(m_pCurrentDrawRoadItem)
//    {
//        m_pCurrentDrawRoadItem->setPoint(point);
//    }
//}

void MainWindow::on_mouseRelease(QPoint point)
{
//    m_pCurrentDrawRoadItem->setPoint(point);
}

void MainWindow::on_mouseClicked(QPointF pointf)
{
    if(m_bDrawRoad)
    {
        if(m_pCurrentDrawRoadItem != nullptr)
        {
            m_pCurrentDrawRoadItem->addPoint(pointf);
            ui->mainWndView->update();
        }

        if(m_pCurrentDrawRectRoadItem != nullptr)
        {
            m_pCurrentDrawRectRoadItem->setPoint(pointf);
            ui->mainWndView->update();
        }

//        if(m_pCurrentDrawLinkItem != nullptr)
//        {
//            m_pCurrentDrawLinkItem->addPoint(pointf);
//            ui->mainWndView->update();
//        }

    }
}

void MainWindow::onSendRect(QRectF qRectf)
{
    emit setLinkRect(qRectf);

    ui->mainWndView->update();
}

void MainWindow::on_ActionMeanAbout()
{
    qDebug() << "MainWindow::on_ActionMeanAbout";
}
